package com.lwx.sys.controller;


import com.lwx.sys.constant.Constant;
import com.lwx.sys.domain.User;
import com.lwx.sys.service.UserService;
import com.lwx.sys.utils.DataGridView;
import com.lwx.sys.utils.EnvUtil;
import com.lwx.sys.utils.MD5Utils;
import com.lwx.sys.utils.ResultObj;
import com.lwx.sys.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * 用户管理
 * @author lwx
 * @since 2020-04-03
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 加载所有用户
     * @param userVo
     * @return
     */
    @GetMapping("/loadAllUser")
    public DataGridView loadAllUser(UserVo userVo){

        return userService.loadAllUser(userVo);
    }

    /**
     * 删除用户
     * @param id
     * @return
     */
    @DeleteMapping("/deleteUser")
    public ResultObj deleteUser(Integer id){
        try {
            userService.deleteUser(id);
            return ResultObj.DELETE_SUCESS;
        }catch (Exception e){
            return ResultObj.DELETE_ERROR;
        }
    }

    //批量删除

    @DeleteMapping("/batchDeleteUser")
    public ResultObj batchDeleteUser(Integer[] ids){
        try {
            userService.batchDeleteUser(ids);
            return ResultObj.DELETE_SUCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }

    /**
     * 添加用户
     * @param user
     * @return
     */
    @PostMapping("/addUser")
    public ResultObj addUser(User user){
        try {
            user.setHiredate(new Date());
            //设置用户类型
            user.setType(Constant.USER_TYPE_NORMAL);
            //设置盐
            user.setSalt(MD5Utils.getSalt());
            //设置密码，2是哈希次数
            user.setPwd(MD5Utils.md5(user.getPwd(),user.getSalt(), EnvUtil.getInt("shiro.hash-iterations")));
            userService.addUser(user);
            return ResultObj.ADD_SUCESS;
        }catch (Exception e){
            return ResultObj.ADD_ERROR;
        }
    }

    /**
     * 更新user
     * @param user
     * @return
     */
    @PostMapping("/updateUser")
    public ResultObj updateUser(User user){
        try {
            userService.updateUser(user);
            return ResultObj.ADD_SUCESS;
        }catch (Exception e){
            return ResultObj.ADD_ERROR;
        }
    }

    @PostMapping("/loadUserMaxOrderNum")
    public DataGridView loadUserMaxOrderNum(){
        Integer max = userService.queryUserMaxOrderNum();
        return new DataGridView(max+1);

    }

    /**
     * 根据部门id查询用户
     * @param deptId
     * @return
     */
    @GetMapping("/queryUserByDeptId")
    public DataGridView queryUserByDeptId(Integer deptId){
        List<User> userList=userService.queryUserByDeptId(deptId);
        return new DataGridView(userList);
    }

    /**
     * 通过userid查询用户
     * @param userId
     * @return
     */
    @GetMapping("/loadUserByUserId")
    public DataGridView loadUserByUserId(Integer userId){
        User  user=userService.queryUserByUserId(userId);
        return new DataGridView(user);
    }

    /**
     * 保存角色权限
     * @param userId
     * @param rids
     * @return
     */
    @RequestMapping("saveUserRole")
    public ResultObj saveUserRole(Integer userId,Integer[] rids){

        try {
            userService.saveUserRole(userId,rids);
            return ResultObj.UPDATE_SUCESS;
        }catch (Exception e){
            e.printStackTrace();//打印异常
            return ResultObj.UPDATE_ERROR;
        }

    }
    //changeUser
    @RequestMapping("changeUser")
    public ResultObj changeUser(User user,HttpSession session){
        try {
            userService.changeUser(user,session);
            return ResultObj.UPDATE_SUCESS;
        }catch (Exception e){
            e.printStackTrace();//打印异常
            return ResultObj.UPDATE_ERROR;
        }
    }
}

